package com.serecttask.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.serecttask.R;
import com.serecttask.model.Login;

import java.util.HashMap;

import util.KsoapUtil;
import util.PostCallback;
import util.ToastUtils;
import widget.Boolean_Mobile;
import widget.ClearEditText;

/**
 * Created by Administrator on 2016/12/14.
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener{
    private ClearEditText et_login_username;
    private ClearEditText et_login_password;
    private Button btn_login_login;
    private TextView tv_login_register;
    private TextView tv_login_forget_password;

    private String username;
    private String password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initData();
        initWidget();
    }

    private void initView(){
        initTopBar("用户登录");
        et_login_username=(ClearEditText) findViewById(R.id.et_login_username);
        et_login_password=(ClearEditText)findViewById(R.id.et_login_password);
        btn_login_login=(Button)findViewById(R.id.btn_login_login);
        tv_login_register=(TextView)findViewById(R.id.tv_login_register);
        tv_login_forget_password=(TextView)findViewById(R.id.tv_login_forget_password);

    }

    private void initData(){

    }

    private void initWidget(){
        btn_login_login.setOnClickListener(this);
        tv_login_forget_password.setOnClickListener(this);
        tv_login_register.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_login_login:
                username =et_login_username.getText().toString();
                password =et_login_password.getText().toString();
                if (TextUtils.isEmpty(username)) {
                    ToastUtils.show(LoginActivity.this, "手机号不能为空", 1);
                } else if (TextUtils.isEmpty(password)) {
                    ToastUtils.show(LoginActivity.this, "密码不能为空", 1);
                } else if (!Boolean_Mobile.isMobileNO(username)) {
                    ToastUtils.show(LoginActivity.this, "请输入正确的手机号", 1);
                } else {
                    loginIn(username, password);
                }
                break;
            case R.id.tv_login_forget_password:
                startActivity(new Intent(LoginActivity.this,ForgetActivity.class));
                break;
            case R.id.tv_login_register:
                startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
                break;
            case R.id.ll_top_Back:
                finish();
                break;
            default:
                break;
        }
    }

    private void loginIn(String username,String password){
        HashMap<String,String> params=new HashMap<>();
        params.put("tname",username);
        params.put("tpwd",password);
        KsoapUtil ksoapUtil=new KsoapUtil("login", params, new PostCallback() {
            @Override
            public void onSuccess(String result) {
                Login login = JSON.parseObject(result, Login.class);
                if(login.isSuccess()){
                    ToastUtils.show(LoginActivity.this, "登录成功", 1);
                    Log.e("Login", login.toString());
                }else {
                    ToastUtils.show(LoginActivity.this, login.getMsg(), 1);
                }
            }

            @Override
            public void onFail(Exception e) {
                ToastUtils.show(LoginActivity.this, "网络异常", 1);
                e.printStackTrace();
            }
        });
        ksoapUtil.execute();
    }
}
